# coding: utf-8
# author: ZhenbPeng
#   date: 2017/10/17 14:14
import logging
import time
import threadpool

from utils.redis_cache import ProxyPool, ProxyStats
from proxy.tasks import ProxyGet, clean_proxyip
from settings import Setting

logger = logging.getLogger("default")


def ip_check(ip_port):
    ip, port = ip_port.split(":")
    if clean_proxyip(ip, port):
        ProxyPool().rpush({"ip": ip, "port": port, "times": 0, "timestamp": 0})
        ProxyStats().add("ip_clean", 1)
        logger.info("proxy module add a new proxy [%s] ProxyPool length=%s" % (ip_port, ProxyPool().length))


def proxy_run():
    THREAD_POOL = threadpool.ThreadPool(Setting.THREAD_NUM)
    logger.debug("proxy module created threadpool[%s]" % Setting.THREAD_NUM)

    while True:
        try:
            if ProxyPool().length < ProxyPool.LENGTH:
                proxy = ProxyGet()
                proxy.get_proxylist()

                if proxy.ip_list:
                    req = threadpool.makeRequests(ip_check, proxy.ip_list)
                    [THREAD_POOL.putRequest(x) for x in req]
                    THREAD_POOL.wait()

            time.sleep(1)
        except Exception as e:
            logger.error("proxy module error %s" % e)


def proxy_start():
    try:
        logger.info("proxy module starting...")
        proxy_run()
    except Exception as e:
        logger.error("proxy module error %s" % e)
